﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using System.Data;
namespace proyecto1_bases2
{
    public class Dbms
    {
        String usuario;
        String password;
        string cadenaconexion;
        String source;
        OracleConnection conneccion;

        public Dbms() {
            source = "(DESCRIPTION="
                     + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))"
                     + "(CONNECT_DATA=(SERVER=DEDICATED)))";
            this.usuario = "josedb";
            this.password = "jmga1987";
            forma_cadena_conexion();
        
        }
        public Dbms(String name,String pass) {
            source = "(DESCRIPTION="
                         + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))"
                         + "(CONNECT_DATA=(SERVER=DEDICATED)))";
            this.usuario = name;
            this.password = pass;
            forma_cadena_conexion();
         
        
        }

        private void forma_cadena_conexion() {

            cadenaconexion = "Data Source=" + source + ";"
                  + "User Id=" + usuario + ";Password=" + password + ";";
        }

        public bool conectar() {
           try
           {
                this.conneccion = new OracleConnection(); // C#
                conneccion.ConnectionString = cadenaconexion;
                conneccion.Open();
                return true;
          }
           catch (Exception) {
               this.conneccion = null;
               return false;
           }
        }

        public bool executeprocedure(String comando) {


            try
            {
                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conneccion;
                cmd.CommandText = comando;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception) { return false; }
        }

        public bool desconectar() {
              try
           {
            this.conneccion.Close();
            this.conneccion = null;
            return true;
           }
              catch (Exception)
              {
                  this.conneccion = null;
                  return false;
              }
        }


    }
}